import { tmpArray } from "./store";
export const filterTreeEmptyObjectData = (treeData: any) => {
  return treeData.filter((item: any) => {
    if (isObject(item) && isEmptyObject(item)) return false;
    if (item?.children?.length) {
      item.children = filterTreeEmptyObjectData(item.children);
      return item.children.length >= 0;
    }
    return true;
  });
};
export const tmpDeepTreeDataOptions = (treeData: any) => {
  const data =
    treeData?.map((item: any) => {
      /*if (item.is_menu === 2) {
        item.name = item.name + "【按钮】";
      }
      if (item.is_menu === 3) {
        item.name = item.name + "【接口】";
      }*/

      if (item.is_menu === 3) return {};
      return {
        label: item.name,
        value: item.id,
        is_menu: item.is_menu,
        children: item?.children?.length
          ? tmpDeepTreeDataOptions(item.children)
          : null,
      };
    }) ?? [];
  return data;
};
export const fetchAllMenu = async (options: { value: any }) => {
  try {
    const res: any = await menuApi.queryAllMenu({});
    if (res.code != 200) return message.error(res.message);
    options.value = filterTreeEmptyObjectData(tmpDeepTreeDataOptions(res.data));
    return options.value;
  } catch (error: any) {
    message.error(error.message);
  }
};

export const getTreeDataOptionsId = (array: any) => {
  array?.forEach((item: any) => {
    tmpArray.value.push(item.value);
    item?.children?.length ? getTreeDataOptionsId(item.children) : null;
  });
  return tmpArray.value;
};
